package com.freework.report.xmlreport.groupBean.service;

import java.net.ProxySelector;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.freework.report.base.service.DataBeanI;
import com.freework.report.base.vo.RequestParam;
import com.freework.report.xmlreport.groupBean.dao.ReportDao;
import com.freework.report.xmlreport.groupBean.dao.SQLBuilderBean;
import com.freework.util.listUtil.ListUtil;
import com.freework.util.listUtil.group.AggregateCol;
import com.freework.util.listUtil.group.GroupCol;

public class GroupDataBean implements DataBeanI {

	private AggregateCol[] cols = null;
	private GroupCol[] groupCol = null;
	private SQLBuilderBean[] dbtabls = null;
	private RequestParam[] requestParams = null;
	private ReportDao dao= new ReportDao();
	//gov.mof.framework.dao.MultiDataSource dataSoure=(MultiDataSource) DBUtil.getBeanByName("dataSource");
	public GroupDataBean(AggregateCol[] cols, GroupCol[] groupCol,
			SQLBuilderBean[] dbtabls, RequestParam[] requestParams) {
		super();
		this.cols = cols;
		this.groupCol = groupCol;
		this.dbtabls = dbtabls;
		this.requestParams = requestParams;
	}
	
	public Connection getCon() {
		
		try {
		//	return
			
			ProxySelector.setDefault(null);
			String className = "com.mysql.jdbc.Driver";
			String url = "jdbc:mysql://10.28.110.176:3306/freework?useUnicode=true&amp;characterEncoding=UTF-8";
			Properties prop = new Properties();
			prop.setProperty("user", "root");
			prop.setProperty("password", "root");
			

				try {
					Class.forName(className).newInstance();
				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return DriverManager.getConnection(url, prop);
			
			
			//dataSoure.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
		
	}
	
	
	
	 
	
	public Object getData(Map<String, Map<String, String>> value) {
		Collection list2=null;
		Connection con = getCon();
		Map map = new HashMap();
		try {
			if (dbtabls.length > 1) {
				MyList list = new MyList();
				for (SQLBuilderBean dbTableInfo : dbtabls) {
						list.addAllList(dao.query(dbTableInfo, null, value!=null?value.get(dbTableInfo.getTablename()):map, con));
				}
				list2 = ListUtil.group(list, groupCol, cols);

			} else if (dbtabls.length == 1){
				list2 = ListUtil.group(dao.query(dbtabls[0],null, value!=null?value.get(dbtabls[0].getTablename()):map,con), groupCol, cols);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list2;
	}

	public RequestParam[] getRequestParams() {
		return requestParams;
	}

}
